package cn.drew.org.service.impl;

import cn.drew.basic.utils.PageList;
import cn.drew.org.domain.Department;
import cn.drew.org.mapper.DepartmentMapper;
import cn.drew.org.query.DepartmentQuery;
import cn.drew.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public void add(Department department) {
        departmentMapper.add(department);
    }

    @Override
    public void update(Department department) {
        departmentMapper.update(department);
    }

    @Override
    public void delete(Long id) {
        departmentMapper.delete(id);
    }

    @Override
    public Department findById(Long id) {
        return departmentMapper.findById(id);
    }

    @Override
    public List<Department> findAll() {
        return departmentMapper.findAll();
    }



    @Override
    public PageList<Department> queryByPage(DepartmentQuery departmentQuery) {
        Integer count = departmentMapper.queryByCount(departmentQuery);
        if (count == 0 || count == null){
            return new PageList(0,new ArrayList());
        }else{
            List<Department> departments = departmentMapper.queryByPage(departmentQuery);
            return new PageList<Department>(count,departments);
        }

    }

    @Override
    public void patchDelete(List<Long> ids) {
        departmentMapper.patchDelete(ids);
    }

    @Override
    public List<Department> tree() {
        ArrayList<Department> result = new ArrayList<>();
        List<Department> allDepts = findAll();
        // 将allDepts转成 deptMap 的key：对象的id  value：dept对象
        Map<Long, Department> deptMap = allDepts.stream().collect(Collectors.toMap(Department::getId, dept -> dept));
        // 相当于
//        Map<Long, Department> deptMap = new HashMap<>();
//        for (Department dept : allDepts) {
//            deptMap.put(dept.getId(),dept);
//        }
        for (Department dept : allDepts) {
            if(null==dept.getParentId()){
                result.add(dept);
            }else{
//                Long parentId = dept.getParentId();
//                for (Department parent : allDepts) {
//                    //儿子的parentId和父级的id相等
//                    if(parentId.equals(parent.getId())){
//                        //children属性 没有初始化 会报空指针
//                        List<Department> children = parent.getChildren();
//                          if (CollectionUtils.isEmpty(children)){
//                            parent.setChildren(children);
//                          }
//                        children.add(dept);
//                        break;
//                    }
//                }
                //儿子的parentId和父级的id相等
                Long parentId = dept.getParentId();
                //儿子的parentId = 父类的id
                Department parent = deptMap.get(parentId);
                parent.getChildren().add(dept);

            }
        }

        return result;
    }


}
